package com.zdw.oauth.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.zdw.common.parent.JSONResult;
import com.zdw.oauth.config.international.InternationalUtils;
import com.zdw.oauth.dto.UserAddDto;
import com.zdw.oauth.dto.UserSelectDto;
import com.zdw.oauth.dto.UserUpdDto;
import com.zdw.oauth.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author 卓德文
 * @since 2023-07-17
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 新增用户
     *
     * @return
     */
    @ApiOperation(value = "新增用户")
    @PostMapping("/add")
    @SaCheckLogin
    @SaCheckPermission(value = "user.add", orRole = {"superAdmin", "admin", "user"})
    public JSONResult add(@RequestBody UserAddDto dto) {
        return new JSONResult().markSuccess(InternationalUtils.getInternational("common.success"), userService.add(dto));
    }

    /**
     * 编辑用户
     *
     * @return
     */
    @ApiOperation(value = "编辑用户")
    @PutMapping("/update")
    @SaCheckLogin
    @SaCheckPermission(value = "user.update", orRole = {"superAdmin", "admin", "user"})
    public JSONResult update(@RequestBody UserUpdDto dto) {
        return new JSONResult().markSuccess(InternationalUtils.getInternational("common.success"), userService.update(dto));
    }

    /**
     * 删除用户
     *
     * @return
     */
    @ApiOperation(value = "删除用户")
    @DeleteMapping("/delete")
    @SaCheckLogin
    @SaCheckPermission(value = "user.delete", orRole = {"superAdmin", "admin", "user"})
    public JSONResult delete(@RequestParam int id) {
        return new JSONResult().markSuccess(InternationalUtils.getInternational("common.success"), userService.delete(id));
    }

    /**
     * 查询用户信息
     *
     * @return
     */
    @ApiOperation(value = "查询用户信息")
    @PostMapping("/select")
    @SaCheckPermission(value = "user.select", orRole = {"superAdmin", "admin", "user"})
    public JSONResult select(@RequestParam int id) {
        return new JSONResult().markSuccess(InternationalUtils.getInternational("common.success"), userService.select(id));
    }

    /**
     * 分页查询用户列表
     *
     * @return
     */
    @ApiOperation(value = "分页查询用户列表")
    @PostMapping("/page")
    @SaCheckPermission(value = "user.select", orRole = {"superAdmin", "admin", "user"})
    public JSONResult page(@RequestBody UserSelectDto dto) {
        return new JSONResult().markSuccess(InternationalUtils.getInternational("common.success"), userService.page(dto));
    }

}
